package annotation;

import java.lang.reflect.Field;

/**
 * @author Jason Black
 * @version The past cannot be redeemed, the future can be changed.
 * @CreateTime 2023/6/22 09:04:19
 **/
@SuppressWarnings({"all"})
public class TestAnnotation {


    /**
     * 通过反射的方式实现获取注解中的值
     *
     * @param args
     * @throws NoSuchFieldException
     */
    public static void main(String[] args) throws NoSuchFieldException {
        // 反射方式获取类
        Class<Teacher> teacherClass = Teacher.class;
        // 通过类获取指定字段
        Field username = teacherClass.getField("username");
        // 通过字段获取字段上的注解
        Username annotation = username.getAnnotation(Username.class);
        // 输出字段
        System.out.println(username);
        // 获取并输出字段注解的值
        System.out.println(annotation.username());
    }

    class Teacher {

        public Teacher() {

        }

        public Teacher(String username, Integer age, String sex) {
            this.username = username;
            this.age = age;
            this.sex = sex;
        }

        @Username
        public String username;
        public Integer age;
        public String sex;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }
    }
}
